//
//  PickViewDialog.m
//  Test
//
//  Created by yang qing on 2018/3/6.
//  Copyright © 2018年 yang qing. All rights reserved.
//

#import "PickViewDialog.h"
#import "UIColor+Hex.h"
@interface PickViewDialog()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (strong,nonatomic)UIPickerView *pickView;
@property (strong,nonatomic) NSArray *datas;
@property (assign,nonatomic) NSInteger index;
@property (strong,nonatomic) NSString *value;
@property (strong,nonatomic) SelectBlock select;
@end
@implementation PickViewDialog


- (instancetype)initWithData:(NSArray *)datas title:(NSString *)tips select:(SelectBlock)selectBlock{
    if(self = [super init]){
        _select = selectBlock;
        //初始化背景颜色
        self.frame = [[UIScreen mainScreen] bounds];
        self.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.5];
        _datas = datas;
        //默认选中第一个
        _index = 0;
        _value = [_datas objectAtIndex:_index];
        
        self.pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,  [[UIScreen mainScreen] bounds].size.height -200, [[UIScreen mainScreen] bounds].size.width, 200)];
        [self.pickView setBackgroundColor:[UIColor whiteColor]];
        self.pickView.delegate = self;
        self.pickView.dataSource = self;
        [self addSubview:self.pickView];
        
        UIView *topView = [[UIView alloc] init];
        topView.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 220, [[UIScreen mainScreen] bounds].size.width, 40);
        topView.backgroundColor = [UIColor colorWithHexString:@"#e8e8e8"];
        [self addSubview:topView];
        
        UIButton *cancelBtn =  [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(0, topView.center.y-topView.frame.origin.y-10, 100, 20);
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
        
        
        UIButton *confirm =  [UIButton buttonWithType:UIButtonTypeCustom];
        confirm.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width -100, topView.center.y-topView.frame.origin.y-10, 100, 20);
        [confirm addTarget:self action:@selector(confirmClick) forControlEvents:UIControlEventTouchUpInside];
        [confirm setTitle:@"完成" forState:UIControlStateNormal];
        [confirm setTitleColor:[UIColor colorWithHexString:ThemeColor] forState:UIControlStateNormal];
        [topView addSubview:cancelBtn];
        [topView addSubview:confirm];
        
        
        UILabel *tipLab = [[UILabel alloc] init];
        [tipLab setText:tips];
        //    1.获取宽度，获取字符串不折行单行显示时所需要的长度
        CGSize titleSize = [tipLab.text sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(MAXFLOAT, 30)];
        tipLab.frame = CGRectMake(0, 0, titleSize.width,titleSize.height);
        tipLab.center = CGPointMake(topView.center.x, topView.center.y-topView.frame.origin.y);
        [tipLab setTextColor:[UIColor grayColor]];
        [topView addSubview:tipLab];
    }
    return self;
}
- (void)cancelClick{
    [self hide];
}


- (void)confirmClick{
    if(_select){
        _select(self,_index,_value);
    }
}

- (void)show{
    UIWindow *window = [[UIApplication sharedApplication]keyWindow];
    [window addSubview:self];
}

- (void)hide{
    if(self.pickView){
        [self removeFromSuperview];
    }
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _datas.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return  [_datas objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    _index = row;
    _value = [_datas objectAtIndex:row];
}

@end
